package com.sean.reverse.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author sean
 * @createTime 2022/4/13 18:07
 * @description
 */
@Configuration
public class ThreadPoolTaskSchedulerConfig {
    @Bean
    public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(3);//配置核心线程数。最大线程数为Integer.MAX_VALUE
        scheduler.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());//配置拒绝策略：调用者所在线程去执行任务
        scheduler.setThreadNamePrefix("reverse-");
//        scheduler.setThreadFactory(new MyThreadFactory("reverse"));
        scheduler.setWaitForTasksToCompleteOnShutdown(true);
        return scheduler;
    }
}
